import { IBullte } from "../Interface/State"
import { Singleton } from "../Singleton"
import { AttributeManager } from "./AttributeManager"
import { GameLogic } from "./GameLogic"

/** 伤害统计 */
export class DamageCountManager extends Singleton {
    static get Ins() {
        return super.GetInstance<DamageCountManager>()
    }

    data: Record<number, any> = {}

    updateDamage(id: number, damage: number) {
        if (!this.data[id]) {
            this.data[id] = damage
        } else {
            this.data[id] += damage
        }
    }


    /** 获取武器计算后的伤害
     * @param bullet 
     * @returns 最终伤害 + 是否暴击
     */
    getDamageByWeapon(bullet: IBullte): { finalDamage: number, isCrit: 0 | 1 } {
        const { damage, critChance, critRate } = bullet
        let finalDamage = Math.round((damage + AttributeManager.Ins.damage) * (1 + AttributeManager.Ins.damageRate))
        let isCrit: 0 | 1 = 0
        if (GameLogic.Ins.getRandom() <= critChance + AttributeManager.Ins.critChance / 100) {
            finalDamage = Math.floor((critRate + AttributeManager.Ins.critRate / 100) * finalDamage)
            isCrit = 1
        }
        return { finalDamage, isCrit }
    }
}